package com.gitee.wsl.net.ssdp.description.model.xml


import com.gitee.wsl.logger.Logger
import com.gitee.wsl.logger.info
import com.gitee.wsl.net.ssdp.description.xml.XmlElement
import kotlin.properties.Delegates

object IconList{
    /**
     * Icon to depict device in a control point UI. Is allowed to have a different value depending on language
     * requested (see ACCEPT-LANGUAGE and CONTENT-LANGUAGE header fields). Icon sizes to support are vendor-specific
     * @param mimetype  - Icon's MIME type (see RFC 2045, 2046, and 2387). Single MIME image type. At least one icon
     *                      should be of type “image/png” (Portable Network Graphics, see IETF RFC 2083).
     * @param width     - Horizontal dimension of icon in pixels. Integer
     * @param height    - Vertical dimension of icon in pixels. Integer
     * @param depth     - Number of color bits per pixel. Integer
     * @param url       - Pointer to icon image. (XML does not support direct embedding of binary data.) Retrieved
     *                      via HTTP. Shall be relative to the URL at which the device description is located in
     *                      accordance with clause 5 of RFC 3986. Specified by UPnP vendor. Single URL
     */
    data class Icon(
        val mimetype: String,
        val width: Int,
        val height: Int,
        val depth: Int,
        val url: String
    ) {
        /**
         * Facilitates flexible deserialization and conditional validation
         */
        companion object {
            fun fromXml(xml: XmlElement): Icon {
                if (xml.tag != "icon") {
                    throw IllegalArgumentException("Unexpected element ${xml.tag} in IconList")
                }
                var mimetypeS by Delegates.notNull<String>()
                var widthI by Delegates.notNull<Int>()
                var heightI by Delegates.notNull<Int>()
                var depthI by Delegates.notNull<Int>()
                var urlS by Delegates.notNull<String>()

                xml.children?.forEach {
                    when (it.tag) {
                        "mimetype" -> mimetypeS = it.get()
                        "width" -> widthI = it.get()
                        "height" -> heightI = it.get()
                        "depth" -> depthI = it.get()
                        "url" -> urlS = it.get()
                        else -> Logger.info("Unexpected tag ${it.tag}")
                    }
                }
                return Icon(mimetypeS, widthI, heightI, depthI, urlS)
            }
        }
    }


    fun fromXml(xml: XmlElement): List<Icon> = xml.children?.map(Icon::fromXml) ?: emptyList()

}